package edu.hcmus.sow.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.hcmus.sow.domain.TaskComment;

/**
 * DAO to manage TaskComment entities.
 * 
 */
@Repository("TaskCommentDAO")
@Transactional

public class TaskCommentDAOImpl extends AbstractHibernateDAO<TaskComment, Integer> implements TaskCommentDAO {
	@Autowired
	TaskDAO taskDAO;
	public List<TaskComment> getCommentsList(Integer taskID) throws DataAccessException{
		//Task task = taskDAO.findByID(taskID);
		String queryString = "from TaskComment as tcm " +
				"join fetch tcm.task t " +
				"join fetch tcm.user tcu " +
				"WHERE t.id = ?";
		List<TaskComment> list = executeQuery(queryString, taskID);
		
		return list;
	}
	
	public int getNumberCommentOfTask(Integer taskID) throws DataAccessException{
	   String queryString = "select count(*) from TaskComment as tc  WHERE tc.task.id = ?";
      Query query = createQuery(queryString, taskID);
      long count = (Long) query.uniqueResult();
      return (int) count;
	}
	
	public TaskComment findByID(Integer id)throws DataAccessException{
	   String queryString = "from TaskComment tc join fetch tc.task t where tc.id = ?";
	   List<TaskComment> list = executeQuery(queryString, id);
	   return list.size() == 0 ? null : list.get(0);
	}
	
}
